﻿
function GetVote(obj) {

    $.ajax({
        type: "get",
        url: "/api/Vote/ShowVote",
        data: {
            Id: obj.data("voteid")
        },
        dataType: "json",
        success: function (data) {
            var array = new Array();
            array.push(data.Title);
            for (var i = 0; i < data.VoteChoiceInfos.length; i++) {
                array.push("<table style=\"table-layout:fixed\">");
                if (data.VoteChoiceInfos[i].VoteType == 1) {
                    array.push("<tr><td><input id=\"RB_" + data.VoteChoiceInfos[i].ID + "\" type=\"radio\" name=\"Selected\" value=\"" + data.VoteChoiceInfos[i].ID + "\"/>" + data.VoteChoiceInfos[i].ChoiceTitle + "</td></tr>");
                }
                else {
                    array.push("<tr><td><input id=\"RB_" + data.VoteChoiceInfos[i].ID + "\" type=\"checkbox\" name=\"Selected\" value=\"" + data.VoteChoiceInfos[i].ID + "\"/>" + data.VoteChoiceInfos[i].ChoiceTitle + "</td></tr>");
                }
            }
            array.push("</table><input type=\"button\" onclick=\"PostVote($(this).parent());\" value=\"提交\" />&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"/Vote/VoteResult.aspx?ID=" + data.ID + "\" target=\"_blank\">查看结果</a>");

            obj.html(array.join('\n'));
        }
    });
}


function PostVote(obj) {
    var datas = {
        ID: obj.data("voteid"),
        VoteChoiceInfos: []
    };
    obj.find("input[name='Selected']").each(function () {
        if (this.checked) {
            datas.VoteChoiceInfos.push({ ID: this.value });
        }
    });

    $.ajax({
        type: "POST",
        url: "/api/Vote/Post",
        data: datas,
        beforeSend: function () {
        },
        success: function (result) {
            alert("投票成功！");
        },
        error: function () {
            alert('PostVote 错误。');
        }

    });
}